package com.muye.adapter;

import java.util.List;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.muye.constant.C;
import com.muye.dianli.R;
import com.muye.field.ShoppingCartField;
import com.zvezda.android.utils.BaseUtils;
import com.zvezda.disply.utils.DisplayConfig;
import com.zvezda.disply.utils.DisplayConst;
import com.zvezda.disply.utils.DisplayManager;

/**
 * 购物车列表适配器
 * 
 * @author TangChao
 *
 */
public class ShoppingCartAdapter extends BaseAdapter
{
	private List<SparseArray<ShoppingCartField>> arrayList;
	
	private Context context;
	
	private String path;
	
	/**
	 * 总价
	 */
	private TextView totalPrice;
	
	/**
	 * 总价
	 */
	private int totalStr;
	
	/**
	 * 支付按钮
	 */
	private TextView payBtn;
	
	/**
	 * 购买的数量数组
	 */
	private int[] count;
	
	public ShoppingCartAdapter(List<SparseArray<ShoppingCartField>> arrayList, Context context, String path)
	{
		super();
		this.arrayList = arrayList;
		this.context = context;
		this.path = path;
	}
	
	@Override
	public int getCount()
	{
		if (arrayList != null)
		{
			return arrayList.size();
		}
		return 0;
	}
	
	@Override
	public SparseArray<ShoppingCartField> getItem(int position)
	{
		if (arrayList != null)
		{
			return arrayList.get(position);
		}
		return null;
	}
	
	@Override
	public long getItemId(int position)
	{
		return position;
	}
	
	@Override
	public View getView(final int position, View convertView, ViewGroup parent)
	{
		try
		{
			count = new int[arrayList.size()];
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		final ViewHolder viewHolder;
		if (convertView == null)
		{
			convertView = LayoutInflater.from(context).inflate(R.layout.shopping_cart_adapter_layout, null);
			convertView.setFocusableInTouchMode(true);
			viewHolder = new ViewHolder();
			viewHolder.imageView = (ImageView) convertView.findViewById(R.id.shopping_cart_adapter_layout_image);
			viewHolder.titleName = (TextView) convertView.findViewById(R.id.shopping_cart_adapter_layout_title);
			viewHolder.content = (TextView) convertView.findViewById(R.id.shopping_cart_adapter_layout_content);
			viewHolder.price = (TextView) convertView.findViewById(R.id.shopping_cart_adapter_layout_price);
			viewHolder.numText = (TextView) convertView.findViewById(R.id.shopping_cart_count_num);
			viewHolder.addBtn = (ImageButton) convertView.findViewById(R.id.shopping_cart_count_add_btn);
			viewHolder.reduceBtn = (ImageButton) convertView.findViewById(R.id.shopping_cart_count_reduce_btn);
			View view = LayoutInflater.from(context).inflate(R.layout.shopping_cart_layout, null);
			// 总价
			totalPrice = (TextView) view.findViewById(R.id.shopping_cart_layout_total_price);
			// 支付按钮
			payBtn = (TextView) view.findViewById(R.id.shopping_cart_layout_pay_btn);
			payBtn.setOnClickListener(new OnClickListener()
			{
				
				@Override
				public void onClick(View v)
				{
					Log.i(C.TAG, "点击了");
				}
			});
			convertView.setTag(viewHolder);
		}
		else
		{
			viewHolder = (ViewHolder) convertView.getTag();
		}
		if (viewHolder != null)
		{
			SparseArray<ShoppingCartField> item = arrayList.get(position);
			viewHolder.titleName.setText(item.get(position).title);
			viewHolder.content.setText(item.get(position).content);
			viewHolder.price.setText("￥： " + item.get(position).price);
			DisplayManager.getInstance().open(context);
			DisplayConfig displayConfig = new DisplayConfig();
			displayConfig.path = BaseUtils.getFilePath(path, item.get(position).image);
			displayConfig.url = item.get(position).image;
			displayConfig.w = 72;
			displayConfig.h = 72;
			displayConfig.zoomModel = DisplayConst.ZOOM_FULL;
			displayConfig.imageView = viewHolder.imageView;
			displayConfig.defaultRes = R.drawable.ic_launcher;
			DisplayManager.getInstance().display(displayConfig);
			/* ============================== */
			viewHolder.numText.setText(count[position] + 1 + "");
			viewHolder.reduceBtn.setOnClickListener(new OnClickListener()
			{
				
				@Override
				public void onClick(View v)
				{
					if (count[position] <= 1)
					{
						return;
					}
					else
					{
						count[position]--;
					}
					viewHolder.numText.setText(count[position] + "");
				}
			});
			viewHolder.addBtn.setOnClickListener(new OnClickListener()
			{
				
				@Override
				public void onClick(View v)
				{
					count[position]++;
					viewHolder.numText.setText(count[position] + "");
				}
			});
			viewHolder.numText.addTextChangedListener(new TextWatcher()
			{
				
				@Override
				public void onTextChanged(CharSequence s, int start, int before, int count)
				{
				}
				
				@Override
				public void beforeTextChanged(CharSequence s, int start, int count, int after)
				{
				}
				
				@Override
				public void afterTextChanged(Editable s)
				{
					totalStr = count[position];
					totalPrice.setText(totalStr + "");
				}
			});
		}
		return convertView;
	}
	
	private class ViewHolder
	{
		ImageView imageView;
		
		TextView titleName;
		
		TextView content;
		
		TextView price;
		
		ImageButton addBtn;
		
		ImageButton reduceBtn;
		
		TextView numText;
		
	}
}
